package clientUI;

import java.awt.BorderLayout;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JTextField;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.GridLayout;
import javax.swing.JLabel;

import client.Client;

import java.awt.Font;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.rmi.RemoteException;
import javax.swing.JPasswordField;

import shared.User;

import java.awt.Color;
import java.io.FileDescriptor;

public class LogonDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private Client client;
	public JTextField textFieldUserName;
	public boolean isOk = false;
	private JPasswordField passwordField;
	private JLabel errorM;
	private JTextField textField;
	private JTextField textField_1;
	/**
	 * Create the dialog.
	 */
	public LogonDialog(Client c)
		{
		this.client = c;
		setBounds(100, 100, 433, 296);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		textFieldUserName = new JTextField();
		textFieldUserName.setBounds(102, 12, 125, 27);
		contentPanel.add(textFieldUserName);
		textFieldUserName.setColumns(10);
		
		JLabel lblUserName = new JLabel("User Name:");
		lblUserName.setFont(new Font("Tahoma", Font.PLAIN, 15));
		lblUserName.setBounds(10, 11, 84, 25);
		contentPanel.add(lblUserName);
		
		JLabel PassLabel = new JLabel("Password:");

		PassLabel.setFont(new Font("Tahoma", Font.PLAIN, 15));
		PassLabel.setBounds(20, 48, 84, 25);
		contentPanel.add(PassLabel);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(102, 50, 125, 25);
		contentPanel.add(passwordField);
		
		JLabel errorMessage = new JLabel("");
		this.errorM = errorMessage;
		errorMessage.setForeground(Color.RED);
		errorMessage.setBounds(30, 86, 342, 48);
		contentPanel.add(errorMessage);
		
		final JButton btnRegister = new JButton("Register");
		btnRegister.setBounds(278, 45, 94, 34);
		contentPanel.add(btnRegister);
		{
			final JButton okButton = new JButton("Logon");
			okButton.setBounds(278, 8, 101, 34);
			contentPanel.add(okButton);
			okButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						client.logon(textFieldUserName.getText(), passwordField.getText());
						isOk = true;
						passwordField.disable();
						passwordField.setBackground(Color.GRAY);
						btnRegister.disable();
						btnRegister.setBackground(Color.GRAY);
						textFieldUserName.disable();
						textFieldUserName.setBackground(Color.GRAY);
						okButton.disable();
						okButton.setBackground(Color.GRAY);
						errorM.setText("Logon is successful");
					} catch (RemoteException e1) {
						// TODO Auto-generated catch block
						int i = e1.getMessage().lastIndexOf(":");
						String s = e1.getMessage().substring(i+2);
						errorM.setText(s);
					}
				}
			});
			okButton.setActionCommand("OK");
			getRootPane().setDefaultButton(okButton);
		}
		{
			JButton cancelButton = new JButton("Exit");
			cancelButton.setBounds(313, 213, 94, 34);
			contentPanel.add(cancelButton);
			cancelButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					dispose();
					client.close();
					System.exit(1);
				}
			});
			cancelButton.setActionCommand("Cancel");
		}
		
		textField = new JTextField();
		textField.setBounds(128, 161, 106, 27);
		contentPanel.add(textField);
		textField.setColumns(10);
		
		JLabel lblFile = new JLabel("File:");
		lblFile.setBounds(102, 162, 36, 24);
		contentPanel.add(lblFile);
		
		textField_1 = new JTextField();
		textField_1.setBounds(313, 161, 94, 27);
		contentPanel.add(textField_1);
		textField_1.setColumns(10);
		
		JLabel lblNewLabel = new JLabel("with user");
		lblNewLabel.setBounds(237, 161, 74, 27);
		contentPanel.add(lblNewLabel);
		
		JButton btnNewButton = new JButton("Share!");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				shared.FileDescriptor fd = client.getCurrentList().get(textField.getText());
				if (!isOk){
					errorM.setText("Not logon yet!");
					return;
				}
				
				try {
					client.shareHandler.requestShare(fd.getFileID(), textField_1.getText());
					errorM.setText("Sharing succeed!");
				} catch (RemoteException e1) {
					int i = e1.getMessage().lastIndexOf(":");
					String s = e1.getMessage().substring(i+2);
					errorM.setText(s);
				}
			}
			
		});
		btnNewButton.setBounds(20, 150, 74, 48);
		contentPanel.add(btnNewButton);
		btnRegister.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				User user = new User();
				user.setUsername(textFieldUserName.getText());
				user.setPassword(passwordField.getText());
				try {
					User newuser = client.signUp(user);
					String s = "new user register. Name: "+newuser.getUsername();
					errorM.setText(s);
				} catch (RemoteException e1) {
					int i = e1.getMessage().lastIndexOf(":");
					String s = e1.getMessage().substring(i+2);
					errorM.setText(s);
				}
			}
		});
	}
}
